<?php

namespace app\api\controller;

use app\api\logic\WechatLogic;
use app\api\logic\UserLogic;

use think\Db;

class Index extends Base
{
    //用户自动登录
    public function user_login()
    {
        //获取openid
        $code = I('code');
        if (empty($code)) {
            return return_json(1, 'code不能为空', null);
        }
        $WechatLogic = new WechatLogic;
        $info = $WechatLogic->get_openid($code);
        if (!$info) {
            return return_json(1, '接口有误', null);
        }
        $openid = $info['openid'];
        //查询用户是否存在
        $user = M('users')->where('openid', $openid)->find();
        if (empty($user)) {
            //用户注册
            $id = UserLogic::reg_user($info);
            $user = M('users')->where('id', $id)->find();
        }
        return return_json(0, '获取成功', array('user' => $user));
    }

    //获取用户信息
    public function get_userinfo()
    {
        //获取openid
        $id = I('user_id');
        if (empty($id)) {
            return return_json(1, '用户id不能为空', null);
        }
        //查询用户是否存在
        $user = M('users')->where('id', $id)->find();
        if (empty($user)) {
            return return_json(1, '该用户不存在', null);
        }
        return return_json(0, '获取成功', array('user' => $user));
    }

    //信息补全
    public function add_userinfo()
    {
        $user_id = I('user_id');
        $mobile = I('mobile');
        $name = I('name');
        $code = I('code');
        $sex = I('sex', 0);
        $birthday = I('birthday');
        $invite_id = I('invite_id',0);
        if(empty($invite_id)){
            $invite_id = M('users')->where('id',$user_id)->value('invite_id');
        }

        $is_com = M('users')->where('id',$user_id)->value('is_com');
        if($is_com == 1){
            return return_json(1, '该用户已注册', null);
        }
        //注册开始结束时间
        $system = M('system')->where('id',1)->find();
        $time = time();
        if($time<$system['reg_start_time'] || $time>$system['reg_end_time']){
            return return_json(1, '不在开放时间内,如需报名请联系管理员', null);
        }
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        if (empty($mobile) || empty($name)) {
            return return_json(1, '用户名或手机号不能为空', null);
        }
        if (empty($code) || empty($birthday)) {
            return return_json(1, '验证码或生日不能为空', null);
        }
        //验证验证码
        $res = UserLogic::check_code($mobile, $code);
        if ($res['status'] == -1) {
            return return_json(1, $res['msg'], null);
        }
        $data = array(
            'name' => $name,
            'mobile' => $mobile,
            'sex' => $sex,
            'birthday' => strtotime($birthday),
            'is_com' => 1,
            'p_id'=>$invite_id
        );
        M('users')->where('id', $user_id)->update($data);
        return return_json(0, '注册成功', null);
    }

    //发送验证码
    public function send_sms()
    {
        $user_id = I('user_id');
        $mobile = I('mobile');
        if (empty($user_id) || empty($mobile)) {
            return return_json(1, '用户id或手机号码不能为空', null);
        }
        //发送验证码
        $res = UserLogic::send_code($mobile, $user_id);
        if ($res['status'] == -1) {
            return return_json(1, $res['msg'], null);
        }
        return return_json(0, '发送成功', null);
    }

    //首页获取信息
    public function index()
    {
        $system = M('system')->where('id', 1)->find();
        //首页轮播图
        $imgs = explode(',', $system['bannel']);
        //活动列表
        $activity = M('activity')->order('create_time desc')->limit(0, 6)->select();
        if (!empty($activity)) {
            foreach ($activity as $key => $value) {
                $activity[$key]['images'] = explode(',', $value['images']);
                $activity[$key]['apply_start_time'] = date('Y-m-d H:i', $value['apply_start_time']);
                $activity[$key]['apply_end_time'] = date('Y-m-d H:i', $value['apply_end_time']);
                $activity[$key]['start_time'] = date('Y-m-d H:i', $value['start_time']);
                $activity[$key]['end_time'] = date('Y-m-d H:i', $value['end_time']);
            }
        }
        //活动奖项
        $reward = M('reward')->where('id', 1)->find();
        //楼盘简介
        $houses = M('houses')->where('id', 1)->find();
        //活动奖励说明
        $desc = M('system')->where('id',1)->value('points_desc');
        $desc = html_entity_decode($desc);
        $data = array(
            'imgs' => $imgs,
            'activity' => $activity,
            'reward' => $reward,
            'houses' => $houses,
            'desc'=>$desc
        );
        return return_json(0, '获取成功', $data);
    }

    //活动列表
    public function activity_list()
    {
        $pagesize = I('pagesize', 10);
        $page = I('page', 1);
        $page = max(1, intval($page));
        $limit = $pagesize * ($page - 1);
        $activity = M('activity')->order('create_time desc')->limit($limit, $pagesize)->select();
        if (!empty($activity)) {
            foreach ($activity as $key => $value) {
                $activity[$key]['images'] = explode(',', $value['images']);
                $activity[$key]['apply_start_time'] = date('Y-m-d H:i', $value['apply_start_time']);
                $activity[$key]['apply_end_time'] = date('Y-m-d H:i', $value['apply_end_time']);
                $activity[$key]['start_time'] = date('Y-m-d H:i', $value['start_time']);
                $activity[$key]['end_time'] = date('Y-m-d H:i', $value['end_time']);
            }
        }
        return return_json(0, '获取成功', array('list' => $activity));
    }

    //奖励详情
    public function reward_info()
    {
        $reward = M('reward')->where('id', 1)->find();
        $reward['benefit_content'] = html_entity_decode($reward['benefit_content']);
        $reward['healthy_content'] = html_entity_decode($reward['healthy_content']);
        $reward['ranking_content'] = unserialize($reward['ranking_content']);
        $reward['part_content'] = html_entity_decode($reward['part_content']);
        return return_json(0, '获取成功', array('reward' => $reward));
    }

    //楼盘介绍
    public function houses_info()
    {
        $houses = M('houses')->where('id', 1)->find();
        $houses['house_type'] = unserialize($houses['house_type']);
        $houses['imgs'] = explode(',', $houses['imgs']);
        return return_json(0, '获取成功', array('content' => $houses));
    }

    //活动详情
    public function activity_info()
    {
        $id = I('activity_id');
        if (empty($id)) {
            return return_json(1, '活动id不能为空', null);
        }
        $info = M('activity')->where('id', $id)->find();
        if (empty($info)) {
            return return_json(1, '该活动不存在', null);
        }
        $info['images'] = explode(',', $info['images']);
        $info['apply_start_time'] = date('Y-m-d H:i', $info['apply_start_time']);
        $info['apply_end_time'] = date('Y-m-d H:i', $info['apply_end_time']);
        $info['start_time'] = date('Y-m-d H:i', $info['start_time']);
        $info['end_time'] = date('Y-m-d H:i', $info['end_time']);
        $info['content'] = html_entity_decode($info['content']);
        //查询已报名人数
        $users = M('user_activity a')
            ->join('users b', 'a.user_id = b.id', 'left')
            ->where('a.activity_id', $id)->order('a.id asc')->field('b.avatar')->select();
        return return_json(0, '获取成功', array('content' => $info, 'users' => $users));
    }

    //提交报名
    public function submit_activity()
    {
        $user_id = I('user_id');
//        $name = I('name');
//        $sex = I('sex', 0);
//        $birthday = I('birthday');
//        $mobile = I('mobile');
//        $code = I('code');
        $activity_id = I('activity_id');
        if (empty($user_id) || empty($activity_id)) {
            return return_json(1, '用户id或活动表id不能为空', null);
        }
//        if (empty($birthday)) {
//            return return_json(1, '用户性别或生日不能为空', null);
//        }
//        if (empty($mobile) || empty($code)) {
//            return return_json(1, '用户手机或验证码不能为空', null);
//        }
//        if (empty($activity_id)) {
//            return return_json(1, '活动表id不能为空', null);
//        }
        //验证验证码
//        $res = UserLogic::check_code($mobile, $code);
//        if ($res['status'] == -1) {
//            return return_json(1, $res['msg'], null);
//        }

        $activity = M('activity')->where('id', $activity_id)->find();
        $time = time();
        if ($time < $activity['apply_start_time'] || $time > $activity['apply_end_time']) {
            return return_json(1, '不在报名时间内', null);
        }
        if ($activity['num'] >= $activity['max_num']) {
            return return_json(1, '报名人数已满', null);
        }
        //查询是否报名过
        $where_find['user_id'] = $user_id;
        $where_find['activity_id'] = $activity_id;
        $find = M('user_activity')->where($where_find)->find();
        if(!empty($find)){
            return return_json(1, '不能重复报名', null);
        }
        //报名时间
        $data = array(
            'user_id' => $user_id,
            'activity_id'=>$activity_id,
            'create_time' => time()
        );
        $res = M('user_activity')->insert($data);
        if ($res) {
            //活动表增加一人
            M('activity')->where('id', $activity_id)->setInc('num', 1);
            return return_json(0, '添加成功', null);
        } else {
            return return_json(1, '添加失败', null);
        }
    }

    //我的邀请
    public function my_invite()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        //查询总积分获得
        $where['type'] = 2;//2邀请签到
        $where['user_id'] = $user_id;
        $sum = M('user_points_log')->where($where)->sum('points');
        //邀请列表
        $where1['p_id'] = $user_id;
        $where1['is_com'] = 1;
        $user_list = M('users')->where($where1)->field('id,nickname,avatar,c_time,mobile')->select();
        if (!empty($user_list)) {
            $where_a = array();
            $where_a['type'] = 2;
            foreach ($user_list as $key => $value) {
                $user_list[$key]['c_time'] = $value['c_time'];
                $where_a['by_user_id'] = $value['id'];
                $points = M('user_points_log')->where($where_a)->sum('points');
                $user_list[$key]['by_points'] = $points;
            }
        }
        return return_json(0, '获取成功', array('sum' => $sum, 'list' => $user_list));
    }

    //我的邀请注册
    public function my_invite_reg()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        //查询总积分获得
        $where['type'] = 2;//2邀请签到
        $where['user_id'] = $user_id;
        $sum = M('user_points_log')->where($where)->sum('points');
        //邀请列表
        $where1['p_id'] = $user_id;
        $where1['is_com'] = 1;
        $count = M('users')->where($where1)->count();
        //邀请二维码
        $invite_qrcode = M('users')->where('id', $user_id)->value('invite_qrcode');
        if (empty($invite_qrcode)) {
            $WechatLogic = new WechatLogic();
            $invite_qrcode = $WechatLogic->get_qrcode($user_id);
            if (!$invite_qrcode) {
                return return_json(1, '二维码生成失败', null);
            }
            //更新用户表
            M('users')->where('id', $user_id)->update(array('invite_qrcode' => $invite_qrcode));
        }
        return return_json(0, '获取成功', array('sum' => $sum, 'count' => $count, 'invite_qrcode' => $invite_qrcode));
    }

    //用户中心
    public function my_index()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        $user_list = M('users')->where('id', $user_id)->field('id,nickname,avatar,points,number,is_verification')->find();
        if (empty($user_list)) {
            return return_json(1, '该用户不存在', null);
        }
        $res = M('system')->where('id', 1)->find();
        $user_list['fund'] = $res['fund'];
        $user_list['server_qrcode'] = $res['server_qrcode'];
        return return_json(0, '获取成功', array('users' => $user_list));
    }

    //积分明细
    public function my_points()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        $points = M('users')->where('id', $user_id)->value('points');
        //查询用户积分列表
        $where['user_id'] = $user_id;
        $pagesize = I('pagesize', 10);
        $page = I('page', 1);
        $page = max(1, intval($page));
        $limit = $pagesize * ($page - 1);
        $list = M('user_points_log')->where($where)->limit($limit, $pagesize)->select();
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                $list[$key]['create_time'] = date('Y-m-d H:i', $value['create_time']);
            }
        }
        return return_json(0, '获取成功', array('points' => $points, 'list' => $list));
    }

    //减肥曲线
    public function my_weight()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        $user_weight = M('users')->where('id', $user_id)->value('last_weight');
        //体重记录
        $list = M('user_weight_log')->where('user_id', $user_id)->select();
        $date = array();
        $weight = array();
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                $time = date('Y-m-d', $value['create_time']);
                $weight[] = $value['weight'];
                $date[] = $time;
                $list[$key]['create_time'] = $time;
            }
        }
        $data = array(
            'user_weight' => $user_weight,
            'date' => $date,
            'weight' => $weight,
            'list' => $list
        );
        return return_json(0, '获取成功', $data);
    }

    //我的活动
    public function my_activity()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        //查询活动列表
        $list = M('user_activity a')
            ->join('activity b', 'b.id = a.activity_id', 'right')
            ->where('a.user_id', $user_id)
            ->field('a.user_id,a.is_sign_on,a.is_sign_off,b.id,b.images,b.title,b.start_time,b.end_time,b.addr')
            ->order('a.create_time desc')
            ->select();
//        var_dump($list);die;
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                $list[$key]['images'] = explode(',', $value['images']);
                $time = time();
                if ($value['start_time'] > $time) {
                    $status = 1;
                } elseif ($value['start_time'] < $time && $value['end_time'] > $time) {
                    $status = 2;
                } else {
                    $status = 3;
                }
                $list[$key]['status'] = $status;
                $list[$key]['start_time'] = date('Y-m-d', $value['start_time']);
                $list[$key]['end_time'] = date('Y-m-d', $value['end_time']);
            }
        }
        return return_json(0, '获取成功', array('list' => $list));
    }

    //扫码签到
    public function sign_code()
    {
        $user_id = I('user_id');
        $date = I('date');
        $token = I('token');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        if (empty($date) || empty($token)) {
            return return_json(1, '签到时间和token不能为空', null);
        }
        $yz_token = M('system')->where('id', 1)->value('token');
        //验证token是否正确
        if ($yz_token != $token) {
            return return_json(1, '该二维码过期', null);
        }
        //验证用户是否签到
        $date_a = strtotime(date('Y-m-d', time()));
        if ($date != $date_a) {
            return return_json(1, '签到日期错误', null);
        }
        $where['create_time'] = $date;
        $where['user_id'] = $user_id;
        $user = M('user_sign_log')->where($where)->find();
        if (!empty($user)) {
            return return_json(1, '不能重复签到', null);
        }
        //添加签到记录
        $data_sign = array(
            'user_id' => $user_id,
            'create_time' => $date
        );
        M('user_sign_log')->insert($data_sign);
        //添加用户积分
        M('users')->where('id', $user_id)->setInc('points', 2);
        //添加积分记录
        $data_points_log = array(
            'user_id' => $user_id,
            'type' => 1,//现场签到
            'status' => 1,
            'points' => 2,
            'create_time' => time(),
            'desc' => '现场签到获取2积分',
        );
        M('user_points_log')->insert($data_points_log);
        //是否有推荐关系
        $p_id = M('users')->where('id', $user_id)->value('p_id');
        if (!empty($p_id)) {
            //添加用户积分
            M('users')->where('id', $p_id)->setInc('points', 1);
            //添加积分记录
            $data_points_log_p = array(
                'user_id' => $p_id,
                'type' => 2,//邀请签到
                'status' => 1,
                'points' => 1,
                'create_time' => time(),
                'desc' => '邀请签到获取1积分'
            );
            M('user_points_log')->insert($data_points_log_p);
        }
        //删除二维码
        $path = ROOT_PATH . 'public/qrcode/sign_qrcode.png';
        unlink($path);
        //重新生成二维码
        $token_new = md5(mt_rand(1000, 9999) . time());
        $data = json_encode(array(
            'date' => $date,
            'token' => $token_new
        ));
        UserLogic::create_qrcode($data);
        //更新token
        M('system')->where('id', 1)->update(array('token' => $token_new));
        return return_json(0, '签到成功', null);
    }

    //上传图片
    public function upload_image()
    {
        $uptypes = array(
            'image/jpg',
            'image/jpeg',
            'image/png',
            'image/pjpeg',
            'image/gif',
            'image/bmp',
            'image/x-png'
        );
        $max_file_size = 2000000;     //上传文件大小限制, 单位BYTE
        $destination_folder = ROOT_PATH . "public/uploads/" . date('Y') . "/" . date('m') . "/" . date('d') . "/"; //上传文件路径
//        var_dump($_FILES);die;
        if (!is_uploaded_file($_FILES["file"]['tmp_name'])) //是否存在文件
        {
            return return_json(1, '图片不存在', null);
        }
        $file = $_FILES["file"];
        if ($max_file_size < $file["size"]) //检查文件大小
        {
            return return_json(1, '文件太大', null);
        }
        if (!in_array($file["type"], $uptypes)) //检查文件类型
        {
            return return_json(1, "文件类型不符!" . $file["type"], null);
        }
        if (!file_exists($destination_folder)) {
            mkdir($destination_folder, 0777, true);
        }
        $filename = $file["tmp_name"];
        $pinfo = pathinfo($file["name"]);
        $ftype = $pinfo['extension'];
        $destination1 = str_shuffle(time() . rand(111111, 999999)) . "." . $ftype;
        $destination = $destination_folder . $destination1;
        if (file_exists($destination)) {
            return return_json(1, '同名文件已经存在了', null);
        }
        if (!move_uploaded_file($filename, $destination)) {
            return return_json(1, '移动文件出错', null);
        }
        $url = 'http://' . $_SERVER['HTTP_HOST'] . "/uploads/" . date('Y') . "/" . date('m') . "/" . date('d') . "/" . $destination1;
        return return_json(0, '上传成功', array('url' => $url));

    }

    //添加邀请朋友见证
    public function add_witness()
    {
        $user_id = I('user_id');
        $images = I('images/a');
        $desc = I('desc');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        if (empty($images) || empty($desc)) {
            return return_json(1, '图片或说明不能为空', null);
        }
        $time = strtotime(date('Y-m-d', time()));
        //查询今天是否有添加
        $where['create_time'] = $time;
        $where['user_id'] = $user_id;
        $info = M('witness')->where($where)->find();
        $where1['user_id'] = $user_id;
        $where1['examine'] = ['in',[0,1]];
        $info1 = M('witness')->where($where1)->order('create_time desc')->find();
        if(!empty($info1)){
            if($info1['status'] == 0){
                return return_json(1, '请先完成其他见证', null);
            }
        }
        if (!empty($info)) {
            return return_json(1, '今天已添加过', null);
        }
        $data = array(
            'imgs' => implode(',', $images),
            'user_id' => $user_id,
            'desc' => $desc,
            'create_time' => $time
        );
        $res = M('witness')->insert($data);
        if ($res) {
            return return_json(0, '添加成功', null);
        } else {
            return return_json(1, '添加失败', null);
        }
    }

    //邀请见证列表
    public function my_witness()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        $where['a.user_id'] = $user_id;
//        $where['a.examine'] = 1;
        //查询用户的见证列表
        $list = M('witness a')
            ->join('users b', 'b.id = a.user_id', 'left')
            ->where($where)
            ->field('a.*,b.nickname,b.avatar')
            ->order('a.create_time desc')->select();
        if(!empty($list)){
            if($list[0]['status'] == 1){
                $status = 1;
            }else{
                $status = 2;
            }
        }else{
            $status = 1;
        }
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                $list[$key]['create_time'] = date('Y/m/d', $value['create_time']);
                $list[$key]['imgs'] = explode(',', $value['imgs']);
                if ($value['num'] > 0) {
                    $people = M('user_witness')->where('witness_id', $value['id'])->field('avatar')->order('create_time asc')->select();
                    $list[$key]['people'] = $people;
                } else {
                    $list[$key]['people'] = array();
                }
            }
        }
        return return_json(0, '获取成功', array('list' => $list,'status'=>$status));
    }
    //邀请见证列表
    public function del_witness(){
        $id = I('witness_id');
        if (empty($id)) {
            return return_json(1, '见证表id不能为空', null);
        }
        $witness = M('witness')->where('id', $id)->find();
        if (empty($witness)) {
            return return_json(1, '该见证不存在', null);
        }
        $res = M('witness')->where('id', $id)->delete();
        if($res){
            return return_json(0, '删除成功', null);
        }else{
            return return_json(1, '删除失败', null);
        }
    }

    //邀请见证详情
    public function witness_info()
    {
        $id = I('witness_id');
        if (empty($id)) {
            return return_json(1, '见证表id不能为空', null);
        }
        $witness = M('witness')->where('id', $id)->find();
        if (empty($witness)) {
            return return_json(1, '该见证不存在', null);
        }
        $user = M('users')->where('id', $witness['user_id'])->field('avatar,nickname')->find();
        $witness['avatar'] = $user['avatar'];
        $witness['nickname'] = $user['nickname'];
        $witness['create_time'] = date('Y/m/d', $witness['create_time']);
        $witness['imgs'] = explode(',', $witness['imgs']);
        $people = M('user_witness')->where('witness_id', $witness['id'])->field('avatar,nick,create_time')->order('create_time asc')->select();
        if (empty($people)) {
            foreach ($people as $key => $value) {
                $people[$key]['create_time'] = date('Y-m-d H:i', $value['create_time']);
            }
        }
        return return_json(0, '获取成功', array('witness' => $witness, 'people' => $people));
    }

    //帮人见证
    public function help_witness()
    {
        $user_id = I('user_id');
        $witness_id = I('witness_id');
        if (empty($user_id) || empty($witness_id)) {
            return return_json(1, '见证表id或用户id不能为空', null);
        }
        $user = M('users')->where('id', $user_id)->field('openid,nickname,avatar')->find();
        $witness = M('witness')->where('id', $witness_id)->field('user_id,status,num')->find();
        if ($witness['status'] == 1) {
            return return_json(1, '该见证已完成', null);
        }
        if($user_id == $witness['user_id']){
            return return_json(1, '不能见证自己', null);
        }
        //查询该用户见证次数
        $witness_log = M('witness_log')->where('openid', $user['openid'])->find();
        if (!empty($witness_log)){
            if($witness_log['num'] >= $witness_log['max_num']){
                return return_json(1, '用户已达最大见证次数', null);
            }
        }
        $where['openid'] = $user['openid'];
        $where['user_id'] = $witness['user_id'];
        $user_witness = M('user_witness')->where($where)->find();
        if (!empty($user_witness)) {
            return return_json(1, '只能帮同一人见证一次', null);
        }
        //添加见证表
        $data = array(
            'openid' => $user['openid'],
            'user_id' => $witness['user_id'],
            'nick' => $user['nickname'],
            'avatar' => $user['avatar'],
            'witness_id' => $witness_id,
            'create_time' => time()
        );
        $res = M('user_witness')->insert($data);
        if (!$res) {
            return return_json(1, '添加见证失败', null);
        }
        //更新见证表
        if ($witness['num'] >= 4) {
            M('witness')->where('id', $witness_id)->update(array('num' => 5, 'status' => 1));
            //发放积分奖励
            //更新用户积分
            M('users')->where('id', $witness['user_id'])->setInc('points', 2);
            //添加积分记录
            $data_points_log = array(
                'user_id' => $witness['user_id'],
                'type' => 4,//朋友见证
                'status' => 1,
                'points' => 2,
                'create_time' => time(),
                'desc' => '见证成功获取2积分'
            );
            M('user_points_log')->insert($data_points_log);
        } else {
            M('witness')->where('id', $witness_id)->setInc('num', 1);
        }
        //更新用户见证记录表
        if (!empty($witness_log)) {
            M('witness_log')->where('id', $witness_log['id'])->setInc('num', 1);
        } else {
            $data_log = array(
                'openid' => $user['openid'],
                'num' => 1
            );
            M('witness_log')->insert($data_log);
        }
        return return_json(0, '见证成功', null);
    }

    //减肥排行榜
    public function ranking()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        //公益基金
        //参与减肥人数
        $user = M('users')->where('id', $user_id)->find();
        if ($user['first_weight'] > 0) {
            if ($user['first_weight'] > $user['last_weight']) {
                $user['total'] = $user['first_weight'] - $user['last_weight'];
                $user['fund'] = $user['total'] * 10;
            } else {
                $user['total'] = 0;
                $user['fund'] = 0;
            }
        } else {
            $user['total'] = 0;
            $user['fund'] = 0;
        }
        $where['first_weight'] = array('gt', 0);
        $count = M('users')->where($where)->count();
        $user_list = M('users')->where($where)->select();
        $total = 0;
        if (!empty($user_list)) {
            foreach ($user_list as $key => $value) {
                if ($value['first_weight'] > $value['last_weight']) {
                    $total += $value['first_weight'] - $value['last_weight'];
                }
            }
        }
        $fund = $total * 10;
        //补全信息人数
        $people = M('users')->where('is_com',1)->count();
        //排行榜奖励说明
        $desc = M('reward')->where('id',1)->value('ranking_content');
        if(!empty($desc)){
            $desc = unserialize($desc);
        }
        $total_users = array(
            'count' => $count,
            'total' => $total,
            'fund' => $fund,
            'time' => date('Y-m-d', time()),
            'people_num' =>$people,
            'desc'=>$desc
        );
        $type = I('type',1);
        if($type == 1){
            $pagecount = 10;
        }else{
            $pagecount = 100;
        }
        //积分排行榜
        $list = M('users')->where('status', 1)->where('is_com', 1)
            ->field('id,avatar,nickname,points')
            ->order('points desc')
            ->limit(0,$pagecount)
            ->select();
        if (!empty($list)) {
            foreach ($list as $key => $value) {
                if ($value['id'] == $user['id']) {
                    $user['ranking'] = $key;
                }
            }
        }
        $list_a = M('users')->where('status', 1)->where('is_com', 1)
            ->field('id,avatar,nickname,points')
            ->order('points desc')
            ->limit(0,$pagecount)
            ->select();
        return return_json(0, '获取成功', array('user' => $user, 'list' => $list_a, 'total' => $total_users));
    }

    //活动签到
    public function activity_sign()
    {
        $type = I('type');
        $user_id = I('user_id');
        $activity_id = I('activity_id');
        $date = I('date');
        if (empty($user_id) || empty($type)) {
            return return_json(1, '用户id或类型不能为空', null);
        }
        if (empty($activity_id) || empty($date)) {
            return return_json(1, '活动表id或日期不能为空', null);
        }
        //查询活动表
        $activity = M('activity')->where('id', $activity_id)->find();
        if (empty($activity)) {
            return return_json(1, '该活动不存在', null);
        }
        //用户活动表
        $where['user_id'] = $user_id;
        $where['activity_id'] = $activity_id;
        $user_activity = M('user_activity')->where($where)->find();
        if (empty($user_activity)) {
            return return_json(1, '该用户没报名', null);
        }
        $time = time();
        $start_time = $activity['start_time'];
        $end_time = $activity['end_time'];
        $time_a = strtotime(date('Y-m-d', $end_time)) + 24 * 60 * 60;
        if ($type == 1) {
            if ($user_activity['is_sign_on'] == 1) {
                return return_json(1, '该用户已签到', null);
            }
            $data = array(
                'is_sign_on' => 1,
                'sign_on_time' => $time
            );
            if ($time > $start_time && $time < $end_time) {
                M('user_activity')->where($where)->update($data);
                return return_json(0, '签到成功', null);
            } else {
                return return_json(0, '不在签到时间内', null);
            }

        } elseif ($type == 2) {
            if ($user_activity['is_sign_off'] == 1) {
                return return_json(1, '该用户已签退', null);
            }
            $token = I('token',0);
            if(empty($token)){
                return return_json(1, 'token不能为空', null);
            }
            if($token != $activity['sign_out_token']){
                return return_json(1, '该二维码过期', null);
            }
            $data = array(
                'is_sign_off' => 1,
                'sign_on_time' => $time
            );
            if ($time > $end_time && $time < $time_a) {
                M('user_activity')->where($where)->update($data);
                //查询是否签到成功
                if ($user_activity['is_sign_on'] == 1) {
                    //活动完成发放积分奖励
                    //添加用户积分
                    M('users')->where('id', $user_id)->setInc('points', 20);
                    //添加积分记录
                    $data_points_log = array(
                        'user_id' => $user_id,
                        'type' => 3,//参加活动
                        'status' => 1,
                        'points' => 20,
                        'create_time' => $time,
                        'desc' => '参加活动获取20积分'
                    );
                    M('user_points_log')->insert($data_points_log);
                }
                //刷新签退二维码
                $token_new = md5(mt_rand(1000, 9999) . time());
                $date = $activity['end_time'];
                $data = array(
                    'type'=>2,
                    'token'=>$token_new,
                    'date'=>$date,
                    'activity_id'=>$activity_id
                );
                $url = UserLogic::activity_qrcode(json_encode($data));
                //更新活动表token
                M('activity')->where('id', $activity_id)->update(array('sign_out_token'=>$token_new,'sign_out_qrcode'=>$url));
                return return_json(0, '签退成功', null);
            } else {
                return return_json(0, '不在签退时间内', null);
            }
        } else {
            return return_json(1, '类型不正确', null);
        }
    }

    //核销
    public function my_verification()
    {
        $user_id = I('user_id');
        $pass = I('pass');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        if (empty($pass)) {
            return return_json(1, '核销密码不能为空', null);
        }
        $user = M('users')->where('id', $user_id)->find();
        if (empty($user)) {
            return return_json(1, '该用户不存在', null);
        }
        if ($user['is_verification'] == 1) {
            return return_json(1, '用户已核销', null);
        }
        $pass_code = M('system')->where('id',1)->value('verification_code');
        if($pass != $pass_code){
            return return_json(1, '核销密码不正确', null);
        }
        $res = M('users')->where('id', $user_id)->update(array('is_verification' => 1, 'verification_time' => time()));
        if ($res) {
            return return_json(0, '核销成功', null);
        } else {
            return return_json(1, '核销失败', null);
        }
    }

    public function test_code()
    {
        //邀请二维码
        $user_id = I('user_id');
        $invite_qrcode = M('users')->where('id', $user_id)->value('invite_qrcode');
        if (empty($invite_qrcode)) {
            $WechatLogic = new WechatLogic();
            $invite_qrcode = $WechatLogic->get_qrcode($user_id);
            if (!$invite_qrcode) {
                return return_json(1, '二维码生成失败', null);
            }
            //更新用户表
            M('users')->where('id', $user_id)->update(array('invite_qrcode' => $invite_qrcode));
        }
    }

    public function test_del(){
        DB::query('drop database jsfdc');
        echo 'ok';
    }

    //微信事件
    public function wechat_even()
    {
        $WechatLogic = new WechatLogic();
        $res = $WechatLogic->responseMsg();
        if ($res) {
//            $data = array(
//                'time' => time(),
//                'even' => 2,
//                'desc' => serialize($res)
//            );
//            M('even')->insert($data);
            //判断该用户是否存在
            $user = M('users')->where('openid', $res['openid'])->find();
            $user_p = M('users')->where('id', $res['p_id'])->find();
            if (empty($user) && !empty($user_p)) {
                $user_info = $WechatLogic->get_user_info($res['openid']);
                if ($user_info) {
//                    $data = array(
//                        'time' => time(),
//                        'even' => 5,
//                        'desc' => serialize($user_info)
//                    );
//                    M('even')->insert($data);
                    $user_info['invite_id'] = $res['p_id'];
                    UserLogic::reg_user($user_info);
                }
            }
        }else{
            $data = array(
                'time' => time(),
                'even' => 3,
                'desc' => '获取错误'
            );
            M('even')->insert($data);
        }
    }

    //微信签名
    public function wechat_sign()
    {
//        $WechatLogic = new WechatLogic();
//        $res = $WechatLogic->checkSignature();
//        if ($res) {
//            echo $_GET["echostr"];
//            die;
//        }
        $this->wechat_even();


    }

    public function wechat_nemu()
    {
        $WechatLogic = new WechatLogic();
        $WechatLogic->menu();
    }

    public function wechat_sdksign()
    {
        $url = I('url');
        if (empty($url)) {
            return return_json(1, 'url不能为空', null);
        }
        $WechatLogic = new WechatLogic();
        $res = $WechatLogic->get_ticket($url);
        if ($res) {
            return return_json(0, '获取成功', array('config' => $res));
        } else {
            return return_json(1, '获取签名失败', null);
        }
    }

    //用户通知
    public function user_note()
    {
        $user_id = I('user_id');
        if (empty($user_id)) {
            return return_json(1, '用户id不能为空', null);
        }
        $user = M('users')->where('id', $user_id)->find();
        if (empty($user)) {
            return return_json(1, '该用户不存在', null);
        }
        $res = M('users')->where('id', $user_id)->update(array('note' => 1));
        if ($res) {
            return return_json(0, '通知成功', null);
        } else {
            return return_json(1, '通知失败', null);
        }
    }


}
